$(function () {

    var ParName = null;


    function loadgetParaName() {
        var defered = $.Deferred();
        $.ajax({
            // url: '../json/getParName.json',
            url: baseUrl + 'getParaName',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            // data: { method: 'getParName' },
        })
            .done(function (data) {
                console.log("getParName");
                ParName = data.data;
                console.log(ParName)
                defered.resolve();
            })

            .fail(function () {
                console.log("error");
            })
        return defered.promise();
    }

    $.when(loadgetParaName()).done(
        function () {
            $.ajax({
                // url: '../json/getProcess.json',
                url: baseUrl + 'getProcess',
                type: 'get',
                // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                // data: { method: 'getProcess' },
            })
                .done(function (data) {
                    console.log("getProcess");
                    var data = data.data;
                    var html = "";
                    for (var i = 0; i < data.length; i++) {
                        html += `<option value="${data[i]}">${data[i]}</option>`;
                    }

                    $(html).appendTo('.getProcess');
                }).done(function () {
                var html = "";
                console.log(ParName)
                for (var i = 0; i < ParName.length; i++) {
                    if ($(".getProcess").val() == ParName[i].processName) {
                        for (var j = 0; j < ParName[i].parameterName.length; j++) {
                            html += `<option value="${ParName[i].parameterName[j]}">${ParName[i].parameterName[j]}</option>`;
                        }

                    }
                }
                $(html).appendTo('.getParName');
            })
                .fail(function () {
                    console.log("error");
                })
        }
    );


    $(".getProcess").change(function (event) {
        /* Act on the event */
        console.log($(this).val())
        $(".getParName").empty();
        // console.log(ParName)
        var html = "";
        for (var i = 0; i < ParName.length; i++) {
            if ($(this).val() == ParName[i].processName) {
                for (var j = 0; j < ParName[i].parameterName.length; j++) {
                    html += `<option value="${ParName[i].parameterName[j]}">${ParName[i].parameterName[j]}</option>`;
                }

            }
        }
        $(html).appendTo('.getParName');
    });


    function getResult(parameterName, count, batch, usl, lsl, productlineNum, deviceNum, stationNum) {
        $.ajax({
            // url: '../json/getResult.json',
            url: baseUrl + 'getResult',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: {
                // method: 'getResult',
                // processName: processName,
                parameterName: parameterName,
                count: count,
                batch: batch,
                usl: usl,
                lsl: lsl,
                productlineNum: productlineNum,
                deviceNum: deviceNum,
                stationNum: stationNum
            },
        })
            .done(function (data) {
                console.log("getParName");
                var data = data.data;
                $(".cp").val(data.cp);
                $(".cpk").val(data.cpk);
            })
            .fail(function () {
                console.log("error");
            })

    }

    $(".button button").click(function (event) {
        /* Act on the event */
        var parameterName = $(".getParName").val();
        var count = Number($(".count").val());
        var batch = $(".batch").val();
        var usl = Number($(".usl").val());
        var lsl = Number($(".lsl").val());
        var productlineNum = $(".productlineNum").val();
        var deviceNum = $(".deviceNum").val();
        var stationNum = $(".stationNum").val();


        if (productlineNum == "" || productlineNum == null) {
            productlineNum = null;
        } else {
            productlineNum = Number(productlineNum);
        }

        if (deviceNum == "" || deviceNum == null) {
            deviceNum = null;
        } else {
            deviceNum = Number(deviceNum);
        }

        if (stationNum == "" || stationNum == null) {
            stationNum = null;
        } else {
            stationNum = Number(stationNum);
        }
        getResult(parameterName, count, batch, usl, lsl, productlineNum, deviceNum, stationNum);

    });

})